#include<stdio.h>
#define N 9//数组个数

extern int binsearch(int *a,int key);

int main()
{
	int i,key; 
	char ch;
	int a[N]={1,2,3,6,7,8,9,89,200};//建立有序数组
	while(1){
	printf("please input the key:\n");//输入关键字
	scanf("%d",&key);
 	i = binsearch(a,key);//查询函数
	if(i == -1)
	printf("search error\n");
	else
	printf("search %d at %d\n",key,i);
	while(getchar() != '\n');//吸收回车字符
	printf("continue?(y|Y):\n");//判断是否继续查询
	scanf("%c",&ch);
	if(ch =='y' ||ch == 'Y')
		continue;
	else 
	  	break;
	}
}

int binsearch(int *a,int key)
{
	int i,low,high,mid;
	low =a[0];//下界
	high = a[N-1];//上届
	while(1)//持续查询
	{
		mid= (low +high)/2;
		if(mid == key)
			return mid ;
		else if(mid < key)
		{
			low = mid + 1;//查询的数值大于mid，下届上移
		}else{
			high = mid -1 ;//查询的数值小于mid，上界下移
		}
	}
	return -1;	
}